//
//  DYZChooseSeriesController.m
//  发布车源
//
//  Created by xmy on 16/8/19.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "DYZChooseSeriesController.h"
#import "DSHTTPClient.h"
#import "YYModel.h"
#import "DYZChooseSeriesModel.h"
#import "DYZChooseSeriesChildModel.h"
#import "Masonry.h"
#import "UIColor+Addition.h"
#import "DYZChooseCarModelController.h"
static NSString *cellId = @"cellId";
@interface DYZChooseSeriesController ()
@property(nonatomic,strong)NSArray *dataArray;

@end

@implementation DYZChooseSeriesController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self series];
    self.navigationItem.title = @"选择车系";
     self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
   //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
}


/**
 *  BD1002 宝马
    BD1001 奔驰
 */
//解析车系
- (void)series{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"accountid"];
    [param setValue:self.brandId forKey:@"brandId"];
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/series.json.php" withParam:param withSuccessBlock:^(id data) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *dict = [data objectForKey:@"data"];
        
        NSArray *arr = [dict objectForKey:@"carList"];
        
        for (NSDictionary *dict1 in arr) {
            
        DYZChooseSeriesModel *model = [DYZChooseSeriesModel yy_modelWithDictionary:dict1];
            
        [array addObject:model];
        }
        
        self.dataArray = array;
        [self.tableView reloadData];
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // 根据 section 获取分组模型
    DYZChooseSeriesModel *model = self.dataArray[section];
    
    return model.carlineList.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1. 取 cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    DYZChooseSeriesModel *model = self.dataArray[indexPath.section];
   
    DYZChooseSeriesChildModel *car = model.carlineList[indexPath.row];
    if (indexPath.section ==0 && [self.brandId  isEqual: @"BD1001"])
    {
         cell.textLabel.text = car.carLineName;
    }else{
        cell.textLabel.text = [self.name stringByAppendingFormat:@"%@",car.carLineName];

    }
  [cell.textLabel setTextColor:[UIColor colorWithHex:0x333333]];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}
//组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // 根据 section 获取分组模型的数据
    DYZChooseSeriesModel *model = self.dataArray[section];
    
    return model.label;
}
//组头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc]init];
    [header setBackgroundColor:[UIColor colorWithHex:0xf0f0f0]];
    CGRect re = header.frame;
    re.size.height = 30;
    
    UILabel *label = [[UILabel alloc]init];

    label.textColor = [UIColor colorWithHex:0x666666];
    [label sizeToFit];
    label.font = [UIFont systemFontOfSize:12];
    [header addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(header);
        make.left.offset(10);
    }];
    DYZChooseSeriesModel *model = self.dataArray[section];
    label.text = model.label;
    return header;

}
//cell的点击事件
//cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    DYZChooseCarModelController *carM = [[DYZChooseCarModelController alloc]init];
    
    DYZChooseSeriesModel *model = self.dataArray[indexPath.section];
    
    DYZChooseSeriesChildModel *car = model.carlineList[indexPath.row];
    
    
    carM.carId = car.carLineId;
    carM.label = self.name;
    [self.navigationController pushViewController:carM animated:YES];
    
}

@end
